package com.css.fxfzfxqh.modules.singleIndicatorRiskZoneSet.repository;

import com.css.fxfzfxqh.modules.singleIndicatorRiskZoneSet.model.entity.FxqhSingleIndicatorRiskZoneSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

@Repository
public class SingleIndicatorRiskZoneSetRepository {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 新增
     *
     * @param fxqhSingleIndicatorRiskZoneSet
     */
    public void insert(FxqhSingleIndicatorRiskZoneSet fxqhSingleIndicatorRiskZoneSet) {
        String sql = "INSERT INTO fxqh_single_indicator_risk_zone_set(id, v1, v2, v3, v4, type, create_time, create_user, update_time, update_user) values(:id, :v1, :v2, :v3, :v4, :type, :createTime, :createUser, :updateTime, :updateUser)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(fxqhSingleIndicatorRiskZoneSet);
        namedParameterJdbcTemplate.update(sql, source);
    }

    public static void main(String[] args) {
        System.out.println(Integer.parseInt("001"));
    }
    /**
     * 修改
     *
     * @param fxqhSingleIndicatorRiskZoneSet
     */
    public void updateById(FxqhSingleIndicatorRiskZoneSet fxqhSingleIndicatorRiskZoneSet) {
        String sql = "update fxqh_single_indicator_risk_zone_set set v1 = :v1, v2 = :v2, v3 = :v3, v4 = :v4, type = :type, create_time = :createTime, create_user = :createUser, update_time = :updateTime, update_user = :updateUser where id = :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(fxqhSingleIndicatorRiskZoneSet);
        namedParameterJdbcTemplate.update(sql, source);
    }

    /**
     * 根据Type查询
     *
     * @param type
     * @return
     */
    public FxqhSingleIndicatorRiskZoneSet selectByType(Integer type) {
        String sql = "select * from fxqh_single_indicator_risk_zone_set where type = '" + type + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(FxqhSingleIndicatorRiskZoneSet.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
